﻿using BCCommon;
using BCData.OA.BusinessFlowRecord;
using BCDto.OA.BusinessFlowRecord;
using BCEntity.OA.BusinessFlowRecord;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Text;

namespace BCService.OA.BusinessFlowRecord
{
    public class BusinessFlowRecordService : IBusinessFlowRecordService
    {
        private readonly IBusinessFlowRecordData businessFlowRecordData;

        public BusinessFlowRecordService(IBusinessFlowRecordData businessFlowRecordData)
        {
            this.businessFlowRecordData = businessFlowRecordData;
        }

        public bool Exists(long businessFlowRecordId)
        {
            return this.businessFlowRecordData.Exists(businessFlowRecordId).GetAwaiter().GetResult();
        }

        public BusinessFlowRecordDto Add(BusinessFlowRecordRequestDto requestDto)
        {
            var entity = requestDto.As<BusinessFlowRecordEntity>();

            return this.businessFlowRecordData.Add(entity).GetAwaiter().GetResult().As<BusinessFlowRecordDto>();
        }
        public BusinessFlowRecordDto Update(BusinessFlowRecordPutDto putDto)
        {
            if (!this.Exists(putDto.BusinessFlowRecordId))
            {
                throw new ArgumentException("此记录不存在");
            }
            var entity = putDto.As<BusinessFlowRecordEntity>();
            return this.businessFlowRecordData.Update(entity).GetAwaiter().GetResult().As<BusinessFlowRecordDto>();
        }

        public bool Delete(long businessFlowRecordId)
        {
            if (!this.Exists(businessFlowRecordId))
            {
                throw new ArgumentException("此记录不存在");
            }

            return this.businessFlowRecordData.Delete(businessFlowRecordId).GetAwaiter().GetResult();
        }

        public BusinessFlowRecordDto GetEntity(long businessFlowRecordId)
        {
            return this.businessFlowRecordData.GetEntity(businessFlowRecordId).GetAwaiter().GetResult().As<BusinessFlowRecordDto>();
        }

        public IEnumerable<BusinessFlowRecordDto> GetList()
        {
            return this.businessFlowRecordData.GetList().GetAwaiter().GetResult().As<IEnumerable<BusinessFlowRecordDto>>();
        }

        public PageableList<BusinessFlowRecordDto> Query(string businessFlowModelCode, string referenceNo, bool? isInitialized, bool? isInitFailed, DateTime? start, DateTime? end, int pageIndex, int pageSize)
        {
            var (list, count) = this.businessFlowRecordData.Query(businessFlowModelCode, referenceNo, isInitialized, isInitFailed, start, end, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<BusinessFlowRecordDto>
            {
                Items = list.As<IEnumerable<BusinessFlowRecordDto>>(),
                Count = count,
                PageIndex = pageIndex,
                PageSize = pageSize
            };
        }
    }
}
